<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.youdi365.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:  心云间、凝听 <981248356@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Builder\AdminFormBuilder;
use Admin\Builder\AdminListBuilder;
class UserController extends AdminController {
    protected $userModel;
    function _initialize()
    {
        parent::_initialize();
        $this->userModel = D('User');
    }
    //用户列表
    public function index(){
        // 搜索
        $keyword   = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['uid|user_login'] = array($condition, $condition, '_multi'=>true);
        // 获取所有用户
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $paged=I('get.p/d',1);
        list($data_list,$totalCount) = $this->userModel->getListByPage($map,$paged,'create_time desc','*',20);
        foreach($data_list as $k=>$user){
            $data_list[$k]['role_name']=get_role_info($user['uid'])['title']?:'普通用户';//获取用户名
        }
        $builder = new AdminListBuilder();
        $builder->setMetaTitle('用户管理') // 设置页面标题
                ->addTopButton('addnew')  // 添加新增按钮
                ->addTopButton('resume')  // 添加启用按钮
                ->addTopButton('forbid')  // 添加禁用按钮
                ->addTopButton('recycle')  // 添加回收按钮
                ->addTopButton('delete')  // 添加删除按钮
                ->setSearch('请输入ID/用户名')
                ->keyListItem('uid', 'UID')
                ->keyListItem('avatar', '头像', 'avatar')
                ->keyListItem('nickname', '昵称')
                ->keyListItem('user_login', '用户名')
                ->keyListItem('role_name', '角色')
                ->keyListItem('email', '邮箱')
                ->keyListItem('mobile', '手机号')
                ->keyListItem('create_time', '注册时间', 'time')
                ->keyListItem('status', '状态', 'status')
                ->keyListItem('right_button', '操作', 'btn')
                ->setListDataKey('uid')
                ->setListData($data_list)    // 数据列表
                ->setListPage($totalCount,20) // 数据列表分页
                ->addRightButton('edit')          // 添加编辑按钮
                ->display();
    }
    /**
     * 编辑用户
     */
    public function edit($uid=0) {
        $title=$uid?"编辑":"新增";
        if (IS_POST) {
            // 密码为空表示不修改密码
            if ($_POST['password'] === '') {
                unset($_POST['password']);
            }

            // 提交数据
            $data = $this->userModel->create();
            if ($data) {
                $result = $this->userModel->editData($data,'uid');
                if ($result) {
                    $this->success($title.'成功', U('index'));
                } else {
                    $this->error($title.'失败', $this->userModel->getError());
                }
            } else {
                $this->error($this->userModel->getError());
            }
        } else {
            // 获取账号信息
            if ($uid!=0) {
                $info = D('User')->find($uid);
                unset($info['password']);
            }

            // 使用FormBuilder快速建立表单页面。
            $builder = new AdminFormBuilder();
            $builder->setMetaTitle($title.'用户')  // 设置页面标题
                    ->addFormItem('uid', 'hidden', 'ID', '')
                    ->addFormItem('nickname', 'text', '昵称', '填写一个有个性的昵称吧')
                    ->addFormItem('user_login', 'text', '用户名', '用户名是作为登录名存在')
                    ->addFormItem('password', 'password', '密码', '')
                    ->addFormItem('email', 'email', '邮箱', '')
                    ->addFormItem('mobile', 'number', '手机号', '')
                    ->addFormItem('sex', 'radio', '性别', '',array(0=>'保密',1=>'男',2=>'女',))
                    ->addFormItem('description', 'textarea', '个人说明', '请填写个人说明')
                    ->addFormItem('avatar', 'picture', '头像', '上传个人头像')
                    ->setFormData($info)
                    ->addButton('submit')->addButton('back')    // 设置表单按钮
                    ->display();
        }
    }

    public function do_log(){
        $this->meta_title ='行为记录';
    	$this->display();
    }

    public function do_list(){
        $this->meta_title ='行为列表';
    	$this->display();
    }

    /**
     * 设置用户的状态
     */
    public function setStatus($model = CONTROLLER_NAME){
        $ids = I('request.ids');
        if (is_array($ids)) {
            if(in_array('1', $ids)) {
                $this->error('超级管理员不允许操作');
            }
        }else{
            if($ids === '1') {
                $this->error('超级管理员不允许操作');
            }
        }
        parent::setStatus($model);
    }
}